﻿using System;
using System.Linq;
using System.Web;
using System.Data.Linq;
using SacNetUG.Areas.Forum.Models;
using SacNetUG.Areas.Forum.Models.Abstract;
using SacNetUG.Areas.Forum.Models.Concrete;

namespace SacNetUG.Areas.Forum.Models.Concrete
{
    public class SqlPostsRepository : IPostsRepository
    {
        private Table<Post> postsTable;
        public SqlPostsRepository(string connectionString)
        {
            postsTable = (new DataContext(connectionString)).GetTable<Post>();
        }

        public IQueryable<Post> Posts
        {
            get { return postsTable; }
        }

        public void SaveSummary(Post post)
        {
            postsTable.InsertOnSubmit(post);
            postsTable.Context.SubmitChanges();
        }

        public void CreateNewPostToASubject(Post post)
        {
            postsTable.InsertOnSubmit(post);
            postsTable.Context.SubmitChanges();
        }

        public void SaveModifiedPost(Post post)
        {
            postsTable.Attach(post);    //do we need this?
            postsTable.Context.Refresh(RefreshMode.KeepCurrentValues, post);
            postsTable.Context.SubmitChanges();
        }

        public void IncrementNumberOfPosts(Post post)
        {
            //postsTable.Attach(post);          this Post instance is already attached??  How so?
            postsTable.Context.Refresh(RefreshMode.KeepCurrentValues, post);
            postsTable.Context.SubmitChanges();
        }

        public void DeleteSelectedPost(Post post)
        {
            postsTable.DeleteOnSubmit(post);
            postsTable.Context.SubmitChanges();
        }
        public void FlagSelectedSubjectOrPost(Post post)    //how different from SaveModifiedPost()?
        {
            postsTable.Context.Refresh(RefreshMode.KeepCurrentValues, post);
            postsTable.Context.SubmitChanges();
        }
        public void ApproveFlaggedSubjectOrPost(Post post)  //how different from SaveModifiedPost()?
        {
            postsTable.Context.Refresh(RefreshMode.KeepCurrentValues, post);
            postsTable.Context.SubmitChanges();
        }
    }
}